﻿/**  
* Ver    变更日期             负责人  变更内容
* V0.01  2015/6/24 16:45:41   何泉水    初版
*/
using System;
using System.Data;
using System.Text;
using System.Data.SQLite;
namespace He.DAL
{
	/// <summary>
	/// 数据访问类:channel
	/// </summary>
	public partial class channel
	{
		public channel()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQLite.GetMaxID("id", "he_channel"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from he_channel");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
			parameters[0].Value = id;

			return DbHelperSQLite.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(He.Model.channel model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into he_channel(");
			strSql.Append("model_id,name,title,sub_title,manage_title,manage_url,max_layer,page_size,list_effect,order_by,bind_id,show_page,show_effect,img_url,img_width,img_height,sort_id,show_attrfield,show_category,show_category_img,show_comment,comment_audit,is_thumb,is_lock,is_protected,seo_title,seo_keywords,seo_desc,lang)");
			strSql.Append(" values (");
			strSql.Append("@model_id,@name,@title,@sub_title,@manage_title,@manage_url,@max_layer,@page_size,@list_effect,@order_by,@bind_id,@show_page,@show_effect,@img_url,@img_width,@img_height,@sort_id,@show_attrfield,@show_category,@show_category_img,@show_comment,@comment_audit,@is_thumb,@is_lock,@is_protected,@seo_title,@seo_keywords,@seo_desc,@lang)");
			strSql.Append(";select LAST_INSERT_ROWID()");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@model_id", DbType.Int32,8),
					new SQLiteParameter("@name", DbType.String),
					new SQLiteParameter("@title", DbType.String),
					new SQLiteParameter("@sub_title", DbType.String),
					new SQLiteParameter("@manage_title", DbType.String),
					new SQLiteParameter("@manage_url", DbType.String),
					new SQLiteParameter("@max_layer", DbType.Int32,8),
					new SQLiteParameter("@page_size", DbType.Int32,8),
					new SQLiteParameter("@list_effect", DbType.Int32,8),
					new SQLiteParameter("@order_by", DbType.String),
					new SQLiteParameter("@bind_id", DbType.String),
					new SQLiteParameter("@show_page", DbType.String),
					new SQLiteParameter("@show_effect", DbType.String),
					new SQLiteParameter("@img_url", DbType.String),
					new SQLiteParameter("@img_width", DbType.Int32,8),
					new SQLiteParameter("@img_height", DbType.Int32,8),
					new SQLiteParameter("@sort_id", DbType.Int32,8),
					new SQLiteParameter("@show_attrfield", DbType.Int32,8),
					new SQLiteParameter("@show_category", DbType.Int32,8),
					new SQLiteParameter("@show_category_img", DbType.Int32,8),
					new SQLiteParameter("@show_comment", DbType.Int32,8),
					new SQLiteParameter("@comment_audit", DbType.Int32,8),
					new SQLiteParameter("@is_thumb", DbType.Int32,8),
					new SQLiteParameter("@is_lock", DbType.Int32,8),
					new SQLiteParameter("@is_protected", DbType.Int32,8),
					new SQLiteParameter("@seo_title", DbType.String),
					new SQLiteParameter("@seo_keywords", DbType.String),
					new SQLiteParameter("@seo_desc", DbType.String),
					new SQLiteParameter("@lang", DbType.String)};
			parameters[0].Value = model.model_id;
			parameters[1].Value = model.name;
			parameters[2].Value = model.title;
			parameters[3].Value = model.sub_title;
			parameters[4].Value = model.manage_title;
			parameters[5].Value = model.manage_url;
			parameters[6].Value = model.max_layer;
			parameters[7].Value = model.page_size;
			parameters[8].Value = model.list_effect;
			parameters[9].Value = model.order_by;
			parameters[10].Value = model.bind_id;
			parameters[11].Value = model.show_page;
			parameters[12].Value = model.show_effect;
			parameters[13].Value = model.img_url;
			parameters[14].Value = model.img_width;
			parameters[15].Value = model.img_height;
			parameters[16].Value = model.sort_id;
			parameters[17].Value = model.show_attrfield;
			parameters[18].Value = model.show_category;
			parameters[19].Value = model.show_category_img;
			parameters[20].Value = model.show_comment;
			parameters[21].Value = model.comment_audit;
			parameters[22].Value = model.is_thumb;
			parameters[23].Value = model.is_lock;
			parameters[24].Value = model.is_protected;
			parameters[25].Value = model.seo_title;
			parameters[26].Value = model.seo_keywords;
			parameters[27].Value = model.seo_desc;
			parameters[28].Value = model.lang;

			object obj = DbHelperSQLite.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(He.Model.channel model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update he_channel set ");
			strSql.Append("model_id=@model_id,");
			strSql.Append("name=@name,");
			strSql.Append("title=@title,");
			strSql.Append("sub_title=@sub_title,");
			strSql.Append("manage_title=@manage_title,");
			strSql.Append("manage_url=@manage_url,");
			strSql.Append("max_layer=@max_layer,");
			strSql.Append("page_size=@page_size,");
			strSql.Append("list_effect=@list_effect,");
			strSql.Append("order_by=@order_by,");
			strSql.Append("bind_id=@bind_id,");
			strSql.Append("show_page=@show_page,");
			strSql.Append("show_effect=@show_effect,");
			strSql.Append("img_url=@img_url,");
			strSql.Append("img_width=@img_width,");
			strSql.Append("img_height=@img_height,");
			strSql.Append("sort_id=@sort_id,");
			strSql.Append("show_attrfield=@show_attrfield,");
			strSql.Append("show_category=@show_category,");
			strSql.Append("show_category_img=@show_category_img,");
			strSql.Append("show_comment=@show_comment,");
			strSql.Append("comment_audit=@comment_audit,");
			strSql.Append("is_thumb=@is_thumb,");
			strSql.Append("is_lock=@is_lock,");
			strSql.Append("is_protected=@is_protected,");
			strSql.Append("seo_title=@seo_title,");
			strSql.Append("seo_keywords=@seo_keywords,");
			strSql.Append("seo_desc=@seo_desc,");
			strSql.Append("lang=@lang");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@model_id", DbType.Int32,8),
					new SQLiteParameter("@name", DbType.String),
					new SQLiteParameter("@title", DbType.String),
					new SQLiteParameter("@sub_title", DbType.String),
					new SQLiteParameter("@manage_title", DbType.String),
					new SQLiteParameter("@manage_url", DbType.String),
					new SQLiteParameter("@max_layer", DbType.Int32,8),
					new SQLiteParameter("@page_size", DbType.Int32,8),
					new SQLiteParameter("@list_effect", DbType.Int32,8),
					new SQLiteParameter("@order_by", DbType.String),
					new SQLiteParameter("@bind_id", DbType.String),
					new SQLiteParameter("@show_page", DbType.String),
					new SQLiteParameter("@show_effect", DbType.String),
					new SQLiteParameter("@img_url", DbType.String),
					new SQLiteParameter("@img_width", DbType.Int32,8),
					new SQLiteParameter("@img_height", DbType.Int32,8),
					new SQLiteParameter("@sort_id", DbType.Int32,8),
					new SQLiteParameter("@show_attrfield", DbType.Int32,8),
					new SQLiteParameter("@show_category", DbType.Int32,8),
					new SQLiteParameter("@show_category_img", DbType.Int32,8),
					new SQLiteParameter("@show_comment", DbType.Int32,8),
					new SQLiteParameter("@comment_audit", DbType.Int32,8),
					new SQLiteParameter("@is_thumb", DbType.Int32,8),
					new SQLiteParameter("@is_lock", DbType.Int32,8),
					new SQLiteParameter("@is_protected", DbType.Int32,8),
					new SQLiteParameter("@seo_title", DbType.String),
					new SQLiteParameter("@seo_keywords", DbType.String),
					new SQLiteParameter("@seo_desc", DbType.String),
					new SQLiteParameter("@lang", DbType.String),
					new SQLiteParameter("@id", DbType.Int32,8)};
			parameters[0].Value = model.model_id;
			parameters[1].Value = model.name;
			parameters[2].Value = model.title;
			parameters[3].Value = model.sub_title;
			parameters[4].Value = model.manage_title;
			parameters[5].Value = model.manage_url;
			parameters[6].Value = model.max_layer;
			parameters[7].Value = model.page_size;
			parameters[8].Value = model.list_effect;
			parameters[9].Value = model.order_by;
			parameters[10].Value = model.bind_id;
			parameters[11].Value = model.show_page;
			parameters[12].Value = model.show_effect;
			parameters[13].Value = model.img_url;
			parameters[14].Value = model.img_width;
			parameters[15].Value = model.img_height;
			parameters[16].Value = model.sort_id;
			parameters[17].Value = model.show_attrfield;
			parameters[18].Value = model.show_category;
			parameters[19].Value = model.show_category_img;
			parameters[20].Value = model.show_comment;
			parameters[21].Value = model.comment_audit;
			parameters[22].Value = model.is_thumb;
			parameters[23].Value = model.is_lock;
			parameters[24].Value = model.is_protected;
			parameters[25].Value = model.seo_title;
			parameters[26].Value = model.seo_keywords;
			parameters[27].Value = model.seo_desc;
			parameters[28].Value = model.lang;
			parameters[29].Value = model.id;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from he_channel ");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from he_channel ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public He.Model.channel GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,model_id,name,title,sub_title,manage_title,manage_url,max_layer,page_size,list_effect,order_by,bind_id,show_page,show_effect,img_url,img_width,img_height,sort_id,show_attrfield,show_category,show_category_img,show_comment,comment_audit,is_thumb,is_lock,is_protected,seo_title,seo_keywords,seo_desc,lang from he_channel ");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
			parameters[0].Value = id;

			He.Model.channel model=new He.Model.channel();
			DataSet ds=DbHelperSQLite.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public He.Model.channel DataRowToModel(DataRow row)
		{
			He.Model.channel model=new He.Model.channel();
			if (row != null)
			{
				if(row["id"]!=null && row["id"].ToString()!="")
				{
					model.id=int.Parse(row["id"].ToString());
				}
				if(row["model_id"]!=null && row["model_id"].ToString()!="")
				{
					model.model_id=int.Parse(row["model_id"].ToString());
				}
				if(row["name"]!=null)
				{
					model.name=row["name"].ToString();
				}
				if(row["title"]!=null)
				{
					model.title=row["title"].ToString();
				}
				if(row["sub_title"]!=null)
				{
					model.sub_title=row["sub_title"].ToString();
				}
				if(row["manage_title"]!=null)
				{
					model.manage_title=row["manage_title"].ToString();
				}
				if(row["manage_url"]!=null)
				{
					model.manage_url=row["manage_url"].ToString();
				}
				if(row["max_layer"]!=null && row["max_layer"].ToString()!="")
				{
					model.max_layer=int.Parse(row["max_layer"].ToString());
				}
				if(row["page_size"]!=null && row["page_size"].ToString()!="")
				{
					model.page_size=int.Parse(row["page_size"].ToString());
				}
				if(row["list_effect"]!=null && row["list_effect"].ToString()!="")
				{
					model.list_effect=int.Parse(row["list_effect"].ToString());
				}
				if(row["order_by"]!=null)
				{
					model.order_by=row["order_by"].ToString();
				}
				if(row["bind_id"]!=null)
				{
					model.bind_id=row["bind_id"].ToString();
				}
				if(row["show_page"]!=null)
				{
					model.show_page=row["show_page"].ToString();
				}
				if(row["show_effect"]!=null)
				{
					model.show_effect=row["show_effect"].ToString();
				}
				if(row["img_url"]!=null)
				{
					model.img_url=row["img_url"].ToString();
				}
				if(row["img_width"]!=null && row["img_width"].ToString()!="")
				{
					model.img_width=int.Parse(row["img_width"].ToString());
				}
				if(row["img_height"]!=null && row["img_height"].ToString()!="")
				{
					model.img_height=int.Parse(row["img_height"].ToString());
				}
				if(row["sort_id"]!=null && row["sort_id"].ToString()!="")
				{
					model.sort_id=int.Parse(row["sort_id"].ToString());
				}
				if(row["show_attrfield"]!=null && row["show_attrfield"].ToString()!="")
				{
					model.show_attrfield=int.Parse(row["show_attrfield"].ToString());
				}
				if(row["show_category"]!=null && row["show_category"].ToString()!="")
				{
					model.show_category=int.Parse(row["show_category"].ToString());
				}
				if(row["show_category_img"]!=null && row["show_category_img"].ToString()!="")
				{
					model.show_category_img=int.Parse(row["show_category_img"].ToString());
				}
				if(row["show_comment"]!=null && row["show_comment"].ToString()!="")
				{
					model.show_comment=int.Parse(row["show_comment"].ToString());
				}
				if(row["comment_audit"]!=null && row["comment_audit"].ToString()!="")
				{
					model.comment_audit=int.Parse(row["comment_audit"].ToString());
				}
				if(row["is_thumb"]!=null && row["is_thumb"].ToString()!="")
				{
					model.is_thumb=int.Parse(row["is_thumb"].ToString());
				}
				if(row["is_lock"]!=null && row["is_lock"].ToString()!="")
				{
					model.is_lock=int.Parse(row["is_lock"].ToString());
				}
				if(row["is_protected"]!=null && row["is_protected"].ToString()!="")
				{
					model.is_protected=int.Parse(row["is_protected"].ToString());
				}
				if(row["seo_title"]!=null)
				{
					model.seo_title=row["seo_title"].ToString();
				}
				if(row["seo_keywords"]!=null)
				{
					model.seo_keywords=row["seo_keywords"].ToString();
				}
				if(row["seo_desc"]!=null)
				{
					model.seo_desc=row["seo_desc"].ToString();
				}
				if(row["lang"]!=null)
				{
					model.lang=row["lang"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,model_id,name,title,sub_title,manage_title,manage_url,max_layer,page_size,list_effect,order_by,bind_id,show_page,show_effect,img_url,img_width,img_height,sort_id,show_attrfield,show_category,show_category_img,show_comment,comment_audit,is_thumb,is_lock,is_protected,seo_title,seo_keywords,seo_desc,lang ");
			strSql.Append(" FROM he_channel ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQLite.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM he_channel ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            object obj = DbHelperSQLite.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from he_channel T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQLite.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@tblName", DbType.VarChar, 255),
					new SQLiteParameter("@fldName", DbType.VarChar, 255),
					new SQLiteParameter("@PageSize", DbType.Int32),
					new SQLiteParameter("@PageIndex", DbType.Int32),
					new SQLiteParameter("@IsReCount", DbType.bit),
					new SQLiteParameter("@OrderType", DbType.bit),
					new SQLiteParameter("@strWhere", DbType.VarChar,1000),
					};
			parameters[0].Value = "he_channel";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

